package com.youclk.exam.service.impl;

import com.youclk.exam.entity.OrderStat;
import com.youclk.exam.repository.OrderRepository;
import com.youclk.exam.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderRepository orderRepository;

    @Override
    public void printOrderStat() {
        long start = System.currentTimeMillis();
        ArrayList<OrderStat> list = orderRepository.getStat(20);
        float executeQueryTime = (System.currentTimeMillis() - start) / 1000f;

        StringBuilder sb = new StringBuilder();
        sb.append(String.format("查询数据库耗时：%.3fs\n", executeQueryTime));

        for (OrderStat orderStat : list) {
            sb.append(String.format("款号：%s----名称：%s----价格：%.2f----累计销量：%d----销售过的颜色：%s\n",
                    orderStat.getGoodsNo(),
                    orderStat.getGoodsName(),
                    orderStat.getGoodsPrice(),
                    orderStat.getSumAmount(),
                    mergeRepeat(orderStat.getColors()))
            );
        }

        System.out.println(sb.toString());

    }

    private String mergeRepeat(String content) {
        HashSet<String> set = new HashSet<>(Arrays.asList(content.split(",")));
        content=set.toString();
        return content.substring(1, content.length() - 1);
    }
}
